package com.choosefine.it.basedata.controller;

import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.choosefine.it.base.BaseController;
import com.choosefine.it.basedata.model.UserRole;
import com.choosefine.it.basedata.service.UserRoleService;
import com.choosefine.it.common.def.ResultCode;
import com.choosefine.it.common.utils.DateUtils;
import com.github.pagehelper.PageInfo;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiResponse;
import io.swagger.annotations.ApiResponses;
import tk.mybatis.mapper.util.StringUtil;

@RestController
@RequestMapping("/basedata/userrole")
@Api(value = "用户角色角色相关", description = "顾文伟")
public class UserRoleController extends BaseController {
	Logger logger = LoggerFactory.getLogger(UserController.class);

	@Autowired
	UserRoleService userRoleService;

	/**
	 * 添加用户角色角色
	 * 
	 * @param userRole
	 * @param response
	 * @return
	 * @throws Exception
	 */
	@ApiOperation(value = "存储用户角色", notes = "传入用户角色对象，存储相关用户角色用户角色基础信息")
	@ApiResponses({
			@ApiResponse(code = ResultCode.SUCCESS_CREATED, message = "添加成功返回结果"),
			@ApiResponse(code = ResultCode.FAILED, message = "请求参数错误") })
	@ApiImplicitParams({
		@ApiImplicitParam(name = "userRoleName", value = "用户角色名", required = true, dataType = "String", paramType = "path"),
		@ApiImplicitParam(name = "description", value = "用户角色描述", required = true, dataType = "String", paramType = "path") })
	@RequestMapping(value = "/{userRoleName}/{description}", method = RequestMethod.POST)
	public String save(@PathVariable String userRoleName,@PathVariable String description) {
		String result = null;
		if (userRoleName != null) {
			UserRole userRole = new UserRole(); 
			userRole.setRoleName(userRoleName);
			userRole.setDescription(description);
			userRole.setCtime(DateUtils.getNowTime());
			Integer num = userRoleService.insert(userRole);
			if (num >0) {
				result = responseSuccess(num, ResultCode.SUCCESS_CREATED,
						ResultCode.MSG_CREATE_SUCCESS);
			} else {
				result = responseFail(ResultCode.MSG_ERROR);
			}
		} else {
			result = responseFail(ResultCode.MSG_PARAMS_NULL);
		}
		return result;
	}

	/**
	 * 修改用户角色
	 * 
	 * @param userRole
	 * @param response
	 * @return
	 * @throws Exception
	 */
	@ApiOperation(value = "修改用户角色", notes = "传入用户角色对象，根据ID更改相关用户角色用户角色基础信息")
	@ApiImplicitParams({
		@ApiImplicitParam(name = "userRoleName", value = "用户角色名", required = true, dataType = "String", paramType = "path"),
		@ApiImplicitParam(name = "description", value = "用户角色描述", required = true, dataType = "String", paramType = "path") })
	
	@ApiResponses({
			@ApiResponse(code = ResultCode.SUCCESS_CREATED, message = "添加成功返回结果"),
			@ApiResponse(code = ResultCode.FAILED, message = "请求参数错误") })
	@RequestMapping(value = "/{id}/{userRoleName}/{description}", method = RequestMethod.PUT)
	public String update(@PathVariable Integer id,@PathVariable String userRoleName,@PathVariable String description) {
		String result = null;
		if (userRoleName != null) {
			UserRole userRole = new UserRole(); 
			userRole.setId(id);
			userRole.setRoleName(userRoleName);
			userRole.setDescription(description);
			userRole.setUtime(DateUtils.getNowTime());
			Integer num = userRoleService.update(userRole);
			if (num >0) {
				result = responseSuccess(num, ResultCode.SUCCESS_CREATED,
						ResultCode.MSG_UPDATE_SUCCESS);
			} else {
				result = responseFail(ResultCode.MSG_ERROR);
			}
		} else {
			result = responseFail(ResultCode.MSG_PARAMS_NULL);
		}
		return result;
	}

	@ApiOperation(value = "根据用户角色ID取用户角色信息", notes = "传入主键ID，根据主键ID获取用户角色信息")
	@ApiImplicitParam(name = "id", value = "用户角色ID", required = true, dataType = "Integer", paramType = "path")
	@RequestMapping(value = "/{id}", method = RequestMethod.GET)
	public String getUserById(@PathVariable Integer id) {
		String result = null;
		if (id > 0) {
			UserRole userRole = userRoleService.findByPrimaryKey(id);
			if (userRole != null) {
				result = responseSuccess(userRole);
			} else {
				result = responseFail(ResultCode.MSG_ERROR);
			}
		} else {
			result = responseFail(ResultCode.MSG_PARAMS_NULL);
		}
		return result;
	}
	
	@ApiOperation(value = "根据用户角色uuid取用户角色信息列表", notes = "传入uuid，根据uuid获取用户角色信息")
	@ApiImplicitParam(name = "uuid", value = "uuid", required = true, dataType = "String", paramType = "path")
	@RequestMapping(value = "/uuid/{uuid}", method = RequestMethod.GET)
	public String getUserById(@PathVariable String uuid) {
		String result = null;
		if (StringUtil.isNotEmpty(uuid)) {
			List<UserRole> userRole = userRoleService.findByUuid(uuid);
			if (userRole != null) {
				result = responseSuccess(userRole);
			} else {
				result = responseFail(ResultCode.MSG_ERROR);
			}
		} else {
			result = responseFail(ResultCode.MSG_PARAMS_NULL);
		}
		return result;
	}

	@ApiOperation(value = "根据用户角色ID删除用户角色", notes = "")
	@ApiImplicitParam(name = "id", value = "用户角色ID", required = true, dataType = "Integer", paramType = "path")
	@RequestMapping(value = "/{id}", method = RequestMethod.DELETE)
	public String delete(@PathVariable Integer id) {
		String result=null;
		if(id!=null){
		int num= userRoleService.deleteByPrimaryKey(id);
		if(num>0){
			result=responseSuccess(num);
		}else{
			result=responseFail(ResultCode.MSG_ERROR);
		}
		}else{
			result = responseFail(ResultCode.MSG_PARAMS_NULL);
		}
		return result;
	}

	@ApiOperation(value = "获取用户角色列表", notes = "根据url分页的参数来查询用户角色的信息列表")
	@ApiImplicitParams({
			@ApiImplicitParam(name = "pageNum", value = "起始行", required = true, dataType = "Integer", paramType = "path"),
			@ApiImplicitParam(name = "pageSize", value = "一页显示的行数", required = true, dataType = "Integer", paramType = "path") })
	@RequestMapping(method = RequestMethod.GET, value = "/{pageNum}/{pageSize}", produces = "application/json")
	public String getUserList(@PathVariable Integer pageNum,
			@PathVariable Integer pageSize) {
		String result = null;
		PageInfo<UserRole> userlist = userRoleService.findAllByPage(null,pageNum, pageSize);

		if (userlist != null) {
			result = responseArraySuccess(userlist);
		} else {
			result = responseFail(ResultCode.MSG_ERROR);
		}
		return result;
	}
}
